package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.CollaboratorMapper;
import com.heima.pojo.Collaborator;
import com.heima.pojo.dto.CollaboratorDTO;
import com.heima.pojo.dto.CollaboratorPageDTO;
import com.heima.pojo.vo.CollaboratorVO;
import com.heima.pojo.vo.PageSearchQueryVO;
import com.heima.service.CollaboratorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class CollaboratorServiceImpl implements CollaboratorService {

    @Autowired
    CollaboratorMapper collaboratorMapper;

    @Override
    public void add(CollaboratorDTO dto) {
        //实体类对象
        CollaboratorVO vo = new CollaboratorVO();
        //拷贝
        BeanUtils.copyProperties(dto,vo);
        //新增创建时间，修改时间
        vo.setCreateTime(LocalDateTime.now());
        vo.setUpdateTime(LocalDateTime.now());
        collaboratorMapper.insert(vo);
    }
    /**
     * 分页查询
     * **/
    @Override
    public PageSearchQueryVO query(CollaboratorPageDTO dto) {
        //开启分页
        PageHelper.startPage(dto.getPageIndex(),dto.getPageSize());
        //查询数据库中全部数据
       Page<Collaborator> page = collaboratorMapper.query(dto);
       //set出vo中的数据
        PageSearchQueryVO vo =new PageSearchQueryVO();
        vo.setPageIndex(dto.getPageIndex());
        vo.setPageSize(dto.getPageSize());
        vo.setTotalCount((int) page.getTotal());
        vo.setCurrentPageRecords(page.getResult());
        return vo;
    }
    /**
     * 根据id删除合作商
     * **/
    @Override
    public void deleyeId(Integer id) {
        collaboratorMapper.deleyeId(id);
    }

    @Override
    public void upd(Integer id, CollaboratorDTO dto) {
        Collaborator collaborator = new Collaborator();
        BeanUtils.copyProperties(dto,collaborator);
        collaborator.setId(Long.valueOf(id));
        collaborator.setUpdateTime(LocalDateTime.now());
        collaboratorMapper.upd(collaborator);
    }

    @Override
    public void getId(Integer id) {
        collaboratorMapper.getId(id);
    }
}
